package com.ch.test.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.ch.base.element.BaseAction;

public class PaginationAction extends BaseAction<PaginationActionForm> {
	private final Logger log = Logger.getLogger(this.getClass());
	
	private List qryListForDB;
	private String codeId;
	private String codeName;
	
	public String execute() throws Exception{
		clearFormData();
//		TestActionForm testActionForm = (TestActionForm)this.getSession(ServletActionContext.getRequest().getRequestedSessionId() + "_InitActionForm");
//		log.info("_InitActionForm actionForm.setQuery1List size= "+testActionForm.getQuery1List().size());
		
//		log.info("actionForm.setQuery1List size= "+actionForm.getQuery1List().size());
//		log.info("_InitActionForm actionForm.setQuery1List 1 size= "+testActionForm.getQuery1List().size());
//		log.info("_InitActionForm actionForm.setQuery1List 2 size= "+testActionForm.getQuery1List().size());

//		log.info(actionForm.getQuery1List().size());
		return "init";
	}

	public String qryByCache() throws Exception{
//		log.info("PaginationAction() actionForm.getQryListForCache size= "+actionForm.getQryListForCache().size());
		log.info("codeId = " + codeId);
		log.info("codeName = " + codeName);
		log.info("pageSize = " + pageSize);
		log.info("nextPage = " + nextPage);
		//當actionForm為空時，進行手動查詢
		if(actionForm.getQryListForCache() == null) {
			List list = new ArrayList();
			for(int i = 0; i < 100; i++){
				String[] temp = new String[1]; 
				temp[0] = String.valueOf(i);
				list.add(temp);
			}
			actionForm.setQryListForCache(list);
		}	
		
		log.info("currentPage = " + currentPage);
		log.info("nextPage = " + nextPage);
		
		this.goToNextPage(actionForm.getQryListForCache(), pageSize, nextPage);
		
		
		return "qryByCache";
	}
	
	//預載查詢
	public void preQryByCache() throws Exception{
		List list = new ArrayList();
		for(int i = 101; i < 200; i++){
			String[] temp = new String[1]; 
			temp[0] = String.valueOf(i);
			list.add(temp);
		}
		
		actionForm.setQryListForCache(list);
	}
	

	public String qryByDB() throws Exception{
		qryListForDB = new ArrayList();
		for(int i = 0; i < 100; i++){
			String[] temp = new String[1]; 
			temp[0] = String.valueOf(i);
			qryListForDB.add(temp);
		}
		totalSize = qryListForDB.size();
		pageSize = 10;
		return "qryByDB";
	}

	public List getQryListForDB() {
		return qryListForDB;
	}

	public void setQryListForDB(List qryListForDB) {
		this.qryListForDB = qryListForDB;
	}

	public Logger getLog() {
		return log;
	}

	public String getCodeId() {
		return codeId;
	}

	public void setCodeId(String codeId) {
		this.codeId = codeId;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}
	
	


	

}
